<template>
	<view class="settle-container">
		  <!-- 全选 -->
		 <label class="radio" @click="changeAllGoodsState">
		 	<radio color="#C00000"  :checked="checkedState"  /><text>全选</text>
		 </label>
		  
		  <!-- 合计 -->
		  <view class="money-count-box">
			合计：<text class="money-count">￥{{checkedGoodsAmount}}</text>
		  </view>
		  
		  <!-- 结算 -->
		  <view class="btn-settle" @click="settlement">结算({{checkedCount}})</view>
	</view>
</template>

<script>
	import {mapGetters,mapMutations,mapState} from 'vuex'
	export default {
		name:"settle",
		data() {
			return {
				seconds:3,
				
				Timer:null
			};
		},
		computed:{
			...mapGetters('cart',['checkedCount','checkedState','checkedGoodsAmount']),
			...mapGetters('user',['addstr']),
			...mapState('user',['token']),
			...mapState('cart',['carts'])
		},
		methods:{
			...mapMutations('cart',['updateAllGoodsState']),
			...mapMutations('user',['updateRedirectInfo']),
			changeAllGoodsState(){
				this.updateAllGoodsState(!this.checkedState)
			},
			settlement(){
				if(!this.checkedCount) return uni.$showMsg('请选择要结算的商品！')
				if(!this.addstr) return uni.$showMsg('请选择收货地址！')
				//if(!this.token) return uni.$showMsg('请先登录！')
				if(!this.token) return this.delayNavigate()
				this.payOrder() 
			},
			async payOrder(){
				const orderInfo={
					order_price:0.01,
					consignee_addr:this.addstr,
					goods:this.carts.filter(x=>x.goods_state).map(x=>({goods_id:x.goods_id,goods_number:x.goods_count,goods_price:x.goods_price}))
				}
				
				//发起请求创建订单
			    const {data:res}=await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)//req_unifiedorder
				if(res.meta.status!==200)return uni.$showMsg("创建订单失败")
				const orderNumber=res.message.order_number
				//console.log(orderNumber)
				
				//订单预支付
				 const {data:res2}=await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderNumber})
				 				 console.log(res2)
				 if(res2.meta.status!==200)return uni.$showMsg("订单预支付失败")
				 const payinfo=res2.message.pay
				 //console.log(payinfo)
				 
				 //调用微信支付
				  const [err,succ]= await uni.requestPayment(payinfo)
				  if(err) return uni.$showMsg('订单未支付')
				  const {data:res3}=await uni.$http.post('/api/public/v1/my/orders/chkOrder',{order_number:orderNumber})
				  if(res3.meta.status!==200) return uni.$showMsg('订单未支付')
				  
				  uni.showToast({
				  	title:'订单支付完成!',
					icon:'success'
				  })
			},
			showTips(n){
				uni.showToast({
					icon:'none',
					title:'请先登录再结算！系统将在'+n+'秒后跳转到登录页面。',
					mask:true,
					duration:1500
					
				})
			},
			delayNavigate(){
				this.seconds=3
				this.showTips(this.seconds)
				this.Timer= setInterval(()=>{
					this.seconds--
					if(this.seconds<=0){
						clearInterval(this.Timer)
						uni.switchTab({
							url:'/pages/my/my',	
							success:()=> {
								this.updateRedirectInfo({
									openType:'switchTab',
									from:'/pages/cart/cart'
								})
							}
						})
						
						return
					}
					this.showTips(this.seconds)
				},1000)
			}
		}
	}
</script>

<style lang="scss">
.settle-container{
	position: fixed;
	left: 0;
	bottom: 0;
	display: flex;
	width: 100%;
	height: 50px;
	justify-content: space-between;
	align-items: center;
	padding-left:5px;
	font-size: 14px;
	background-color: white;
	.radio{
		display: flex;
		align-items: center;
	}
	.money-count-box{
		.money-count{
			color: #C00000;
			font-weight: bold;
		}
	}
	.btn-settle{
		background-color: #C00000;
		height: 50px;
		line-height: 50px;
		padding:0 10px;
	}
}
</style>